---[[
--
--
--  取出redis中的dellist id
--  根据id 删除mongodb记录 以及storage
--  清空mongodb中的gfile字段
--  删除storage
--]]
local cjson                    = require "cjson"
local red_method               = require "gcs.list.method"
local mongo_method             = require "gcs.mongodb.method"
local storage                  = require "resty.gcsdfs.storage"
local up_utils                 = require "gcs.upload.upload_utils"
local config_table             = ngx.shared.gcs_config   --声明ngx.shared.gcs_config
local store_connect_timeout    = config_table:get("p_store_connect_timeout")
local store_keep_timeout       = config_table:get("p_store_keep_timeout")
local store_keep_count         = config_table:get("p_store_keep_count")
local tb_value = {
  timeout = store_connect_timeout,
  keep_timeout = store_keep_timeout,
  keep_count = store_keep_count
}

--local utils = require "gcs.utils"
local num = 100 --一次删除的次数
for i=1,num do
  local id,err = red_method.pop("r","dellist")
  --update_data(table_name,update_data,selector)
  if id then
    -------------清空mongodb中的gfile
    local update_data = {gfile=""}
    local selector = {id=id}
    local query = {}
    query.id = id
    local returnfields = {}
    returnfields = {gfile=1,str_res=1}
    local conn,cursor,err = mongo_method.select_data("gcxx",query,returnfields)
    if cursor then
      local k,v = cursor:next()                              
      if v then
        local gfile = v["gfile"]
        local cjson_str_res = v["str_res"]
        local str_res = cjson.decode(cjson_str_res)
        local stroage_tb = {}
        stroage_tb.gfile1 = gfile   --取出gfile
        local n,err = mongo_method.update_data("gcxx",update_data,selector) --清空mongodb中的gfile
        if n then
          local res,err = up_utils.del_storage(str_res,tb_value,stroage_tb)
          if not res then
            ngx.say(err)
          end
        end
      end
      mongo_method.close_db(conn)
    end
  end
end


















